package com.ncu.mixproject;

import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;

import android.Manifest;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.os.StrictMode;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

import java.io.File;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.List;

public class MusicActivity extends AppCompatActivity {
    private ListView listview;
    private List musicList;
    private ArrayAdapter<String> adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music);

        Intent intent = this.getIntent();
        String title =  intent.getStringExtra("title");
        this.setTitle(title);

        if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) == PackageManager.PERMISSION_DENIED) {
            if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.M){
                requestPermissions(
                        new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},1
                );
            }
        }

        listview=(ListView)findViewById(R.id.music_list);
        if(loadMusicList()) {
            //设定列表项的选择模式为单选
            adapter = new ArrayAdapter<String>(MusicActivity.this,
                    android.R.layout.simple_list_item_1, musicList);
            listview.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
            listview.setAdapter(adapter);
        }else{
                Toast.makeText(this, "Music目录下没有音乐文件", Toast.LENGTH_LONG).show();
            }
                listview.setOnItemClickListener( new AdapterView.OnItemClickListener(){
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        String musicName = listview.getItemAtPosition(position).toString();
                        PlayMusic(musicName);
                    }
                });

    }
    public void PlayMusic( String fileName) {
        String internalSdcard = Environment.getExternalStoragePublicDirectory(
                Environment.DIRECTORY_MUSIC).getAbsolutePath() + File.separator;
        String fileFullPath = internalSdcard + fileName;
        if (Build.VERSION.SDK_INT >= 24) {
            try {
                Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure");
                m.invoke(null);
            } catch (Exception e) {
                e.printStackTrace();
            }
            File file = new File(fileFullPath);
            if (file != null) {
                Intent intent = new Intent(Intent.ACTION_VIEW);
                intent.setDataAndType(Uri.fromFile(file), "audio/*");
                try {
                    startActivity(intent);
                } catch (ActivityNotFoundException e) {
                    Log.e("ERROR", "startActivity Error");
                }
            }
        }
    }
    public boolean loadMusicList(){
        File music_dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC);
        File[] files = music_dir.listFiles();
        if(files!=null){
            if(files.length>0){
                musicList= new ArrayList<String>();
                for(int i=0;i<files.length;i++){
                    musicList.add(files[i].getName());
                }
                return true;
            }
        }
        return false;
    }
}
